const {
  get
} = require("../../utils/service")

// pages/success/success.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    okImage: `${app.globalData.host}/static/images/wechat.png`,
    failImage: `${app.globalData.host}/static/images/wechat.png`,
    paid: true,
    icon: {
      type: 'cancel',
      color: '#ff0000'
    },
    buttons: [{
        type: 'positive',
        block: true,
        text: '确定',
      },
      {
        type: 'light',
        block: true,
        text: '继续购买',
      },
    ],
    failButtons: [{
      block: true,
      text: '刷新',
      type: 'positive'
    }, {
      type: 'calm',
      block: true,
      text: '重新支付',
    }, {
      type: 'light',
      block: true,
      text: '继续购买',
    }, ]
  },
  onSuccessClick: function (e) {
    const idx = e.detail.index
    switch (idx) {
      case 0: {
        wx.navigateTo({
          url: '/pages/myorder/myorder',
        })
        break;
      }
      case 1: {
        wx.switchTab({
          url: '/pages/library/library',
        })
        break;
      }
    }
  },
  onFailClick: function (e) {
    console.info(e)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const orderId = options.orderId
    const self = this
    get(`${app.globalData.host}/order/view/${orderId}`, {}, {
      success: function (data) {
        if (data.status === 'paid') {
          self.setData({
            paid: true
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})